import sys
import os
import shutil
from PyQt4 import QtCore, QtGui, Qt
from imageViewer import Ui_MainWindow

class PictureViewer(QtGui.QMainWindow):
    '''
    The main run class.
    '''
    def __init__(self, dirName, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.dirName = dirName
        self.ui = Ui_MainWindow()
        self.running = True
        self.ui.setupUi(self)
        self.listwidget = QtGui.QListWidget()
        self.ui.verticalLayout_3.addWidget(self.listwidget)
        self.listwidget.setIconSize(QtCore.QSize(100,100))
        self.listwidget.itemSelectionChanged.connect(self.on_item_clicked)
        self.populate_images()
        self.deleteImageAction = QtGui.QAction(
            QtGui.QIcon('icons/error.png'),
            '&Delete Image', self)
        self.deleteImageAction.setShortcut(QtGui.QKeySequence("Delete"))
        self.connect(self.deleteImageAction, QtCore.SIGNAL('triggered()'),
                     self.deleteImage)
        self.saveImagesAction = QtGui.QAction(
            QtGui.QIcon('icons/saveIm.png'),
            '&Save remaining images', self)
        self.connect(self.saveImagesAction, QtCore.SIGNAL('triggered()'),
                     self.save_images)
        self.ui.menu_Save_Images.addAction(self.saveImagesAction)
        self.listwidget.setContextMenuPolicy(QtCore.Qt.ActionsContextMenu)
        self.listwidget.addActions([self.deleteImageAction])
        self.ui.label.setText("Select any image on the left to see a larger version")

    def populate_images(self):
        try:
            for i in os.listdir(self.dirName):
                qIcon = QtGui.QIcon()
                qIcon.addPixmap(QtGui.QPixmap(self.dirName + i), QtGui.QIcon.Normal, QtGui.QIcon.Off)
                item = QtGui.QListWidgetItem(i, self.listwidget)
                item.setStatusTip(i)
                item.setIcon(qIcon)
        except:
            pass
            
    def save_images(self):
        try:
            directory = QtGui.QFileDialog.getExistingDirectory(self, 'Select Location for storing the images.')
            for fl in os.listdir(self.dirName):
                shutil.copyfile(self.dirName + fl, directory + '/' + fl)
        except:
            pass            
        
    def get_selected_filename(self):
        try:
            selected_items = self.listwidget.selectedItems()
            if len(selected_items) > 1: return
            selected_text = selected_items[0].text()
            selected_filename = self.dirName + selected_text
            return [selected_items[0], selected_filename]
        except:
            pass
        
    def deleteImage(self):
        try:
            import os
            filenum, filename = self.get_selected_filename()
            filenum = self.listwidget.indexFromItem(filenum).row()
            filename = str(filename)
            os.remove(filename)
            self.listwidget.takeItem(filenum)
        except:
            pass        
        
    def on_item_clicked(self):
        try:
            fileimg = QtGui.QPixmap(self.get_selected_filename()[1])
            self.ui.label.setPixmap(fileimg)
        except:
            pass        

    def closeEvent(self, event):
        for f in os.listdir(self.dirName):
            os.remove(self.dirName + f)
        self.listwidget.clear()
        self.ui.label.setText('')
        self.running = False
        self.ui = None
        self.close()

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    myapp = PictureViewer('delete/')
    myapp.show()
    sys.exit(app.exec_())
